<template>
  <div class="post-comment">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="优质评论将会被优先展示"
      show-word-limit
    />
    <van-button
      type="primary"
      size="small"
      @click="onComment"
      :disabled="!message.length || isFalse"
    >发布</van-button>
  </div>
</template>

<script>
import { addComment } from '@/api/comment'
export default {
  name: 'PostComment',
  components: {},
  props: {
    list: {
      type: Array,
      required: true
    },
    artID: {
      type: [String, Number, Object]
    }
  },
  inject: {
    articleID: {
      type: [String, Object, Number]
    }
  },
  data () {
    return {
      message: '',
      isFalse: false
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onComment () {
      this.isFalse = true
      this.$toast.loading({
        message: '发布中...',
        forbidClick: false, // 禁用背景点击
        duration: 0
      })
      try {
        const { data } = await addComment({
          target: this.artID || this.articleID,
          content: this.message
        })
        this.$toast('发布成功')
        this.isFalse = false
        this.$emit('addComment', data.data.new_obj)
        this.message = ''
      } catch (err) {
        this.$toast('操作失败，请重试')
        this.isFalse = false
      }
    }
  }
}
</script>

<style scoped lang="less">
.post-comment {
  display: flex;
  padding: 15px;
  align-items: flex-end;
  .post-field {
    background: #f5f7f9;
    margin-right: 15px;
  }
}
</style>
